package com.md.album.adapter

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.living.player.PlayUtils
import com.md.album.R
import com.md.album.ui.CURRENT_ALBUM_ID
import com.md.album.ui.SelectSelfAlbumActivity
import com.md.album.ui.SelfAudioBatchActivity
import com.md.network.api.Album
import com.md.network.api.Audio
import com.yxt.function_sdk_share.constants.SINGLE_AUDIO_TYPE
import com.yxt.function_sdk_share.util.BottomPopupUtil

const val TYPE_HEADER = 0x1
const val TYPE_ITEM = 0x2

class AlbumAdapter(var context: Context, var isMine: Boolean, var audios: List<Audio>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private var mContext = context
    lateinit var album: Album
    private var isMineD = isMine

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == TYPE_HEADER) {
            var viewHeader = LayoutInflater.from(context).inflate(R.layout.adapter_audio_add_header, parent, false)
            ViewHolderHeader(viewHeader)
        } else {
            var view = LayoutInflater.from(context).inflate(R.layout.adapter_album_new, parent, false)
            AlbumViewHolder(view)
        }

    }

    override fun getItemCount(): Int {
        return audios.size + 1
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (position == 0) {
            (holder as ViewHolderHeader).apply {
                holder.tvPlayAll!!.text = context.resources.getString(R.string.album_play_all) + "${audios.size}"
                holder.ivPlay!!.setOnClickListener {


                }

                holder.ivSort?.apply {
                    if (isMine) {
                        visibility = View.VISIBLE
                        setOnClickListener {
                            var intent = Intent(context, SelfAudioBatchActivity::class.java)
                            intent.putExtra(SelfAudioBatchActivity.SELF_ALBUM_ID, album?.id)
                            context.startActivity(intent)
                        }
                    } else {
                        visibility = View.GONE
                    }
                }
                holder.ivAddAudio?.apply {
                    if (isMine) {
                        visibility = View.VISIBLE
                        setOnClickListener {
                            var intent = Intent(mContext, SelectSelfAlbumActivity::class.java)
                            intent.putExtra(CURRENT_ALBUM_ID, album?.id)
                            (mContext as AppCompatActivity).startActivity(intent)
                        }
                    } else {
                        visibility = View.GONE
                    }
                }


            }
        } else {
            var curAudio = audios[position - 1]
            var num = when {
                position < 10 -> "00$position"
                position in 10..99 -> "0$position"
                else -> position.toString()
            }
            (holder as AlbumViewHolder).apply {
                holder.tvNum!!.text = num
                holder.tvAuthor!!.text = curAudio.author
                holder.tvTitle!!.text = curAudio.name
                holder.cltRoot!!.setOnClickListener {
                    // VideoActivity.intentTo(context,curAudio.url,curAudio.name)
//                    VideoActivity.intentTo(context,  curAudio)
                    PlayUtils.intentTo(context, album, curAudio)
                }
                holder.fltIvMore?.setOnClickListener{

                    var bottomType = if(isMine) arrayOf(SINGLE_AUDIO_TYPE.ADD,SINGLE_AUDIO_TYPE.DELETE) else arrayOf(SINGLE_AUDIO_TYPE.ADD)
                    BottomPopupUtil.shareWithWindows(context as Activity, bottomType,album.id,curAudio,null)
                }

            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        return if (position == 0) {
            TYPE_HEADER
        } else {
            TYPE_ITEM
        }
    }

    public fun setData(album: Album?, audios: List<Audio>) {
        if (album != null) {
            this.album = album
        }
        this.audios = audios
        notifyDataSetChanged()
    }

    class AlbumViewHolder : RecyclerView.ViewHolder {
        var tvNum: TextView? = null
        var tvTitle: TextView? = null
        var tvAuthor: TextView? = null
        var ivMore: ImageView? = null
        var cltRoot: ConstraintLayout? = null
        var fltIvMore: FrameLayout? = null

        constructor(itemView: View) : super(itemView) {
            tvNum = itemView.findViewById(R.id.tvNum)
            tvTitle = itemView.findViewById(R.id.tvTitle)
            tvAuthor = itemView.findViewById(R.id.tvAuthor)
            ivMore = itemView.findViewById(R.id.ivMore)
            cltRoot = itemView.findViewById(R.id.cltRoot)
            fltIvMore = itemView.findViewById(R.id.fltIvMore)
        }
    }

    class ViewHolderHeader : RecyclerView.ViewHolder {
        var ivAddAudio: ImageView? = null
        var tvPlayAll: TextView? = null
        var ivPlay: ImageView? = null
        var ivSort: ImageView? = null

        constructor(itemView: View) : super(itemView) {
            ivAddAudio = itemView.findViewById(R.id.ivAddAudio)
            tvPlayAll = itemView.findViewById(R.id.tvPlayAll)
            ivPlay = itemView.findViewById(R.id.ivPlay)
            ivSort = itemView.findViewById(R.id.ivSort)
        }
    }
}